package com.rjwh.dingdong.client.activity.download.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.adapter.DowloadLocaListAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.DownloadInfo;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.download.DownloadManager;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.util.FileUtils;
import com.rjwh.dingdong.client.util.RxBus;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.Subscriber;

/**
 * 项目名称：whrjwh-parent
 * 类描述：下载本地目录
 * 创建人：wby
 * 创建时间：2016/6/23 14:32
 * 修改人：wby
 * 修改时间：2016/6/23 14:32
 * 修改备注：
 *
 */
public class DownloadLocalFramgent extends BaseFragment implements AdapterView.OnItemLongClickListener {
    @BindView(R.id.listview)
    ListView listview;
    // TODO: Rename and change types and number of parameters
    DowloadLocaListAdapter adapter;
    private Observable<DownloadInfo> observable;

    public static DownloadLocalFramgent newInstance() {
        DownloadLocalFramgent fragment = new DownloadLocalFramgent();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.comm_listview, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        loadDbData();
        regiest();
    }

    public void initView() {
        adapter = new DowloadLocaListAdapter(getActivity());
        listview.setAdapter(adapter);
        listview.setOnItemLongClickListener(this);
    }

    public void regiest() {
        observable = RxBus.get().register(LocalConstant.RX__POST_DOWNLOAD_STATE_UNZIP, DownloadInfo.class);
        Subscriber subscriber = new Subscriber<DownloadInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(DownloadInfo localGmaeInfo) {
                if (localGmaeInfo.getDownloadState() == DownloadManager.STATE_UNZIP_SUCESS) {
                    loadDbData();
                }
            }
        };
        addSubscription(observable,subscriber);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister("unzip", observable);
    }


    public void loadDbData() {
        adapter.clear();
        List<LocalGmaeInfo> listData = MyApplication.db.findAll(LocalGmaeInfo.class,"type");
        adapter.appendToList(listData);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
        showDelDialog(i,(LocalGmaeInfo) adapter.getItem(i));
        return false;
    }

    public void showDelDialog(final int ps,final LocalGmaeInfo info) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setMessage("是否删除");
        dialog.setNegativeButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
                adapter.delItme(ps);
                MyApplication.db.delete(info);
                FileUtils.deleteFile(new File(info.getPath()));
            }
        });
        dialog.setPositiveButton("否", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        dialog.show();
    }

}
